package com.stylefeng.guns.modular.system.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.annotion.BussinessLog;
import com.stylefeng.guns.core.common.constant.dictmap.NoticeMap;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.modular.system.model.Article;
import com.stylefeng.guns.modular.system.model.ArticleData;
import com.stylefeng.guns.modular.system.model.CmsArticleCategory;
import com.stylefeng.guns.modular.system.service.IArticleDataService;
import com.stylefeng.guns.modular.system.service.IArticleService;
import com.stylefeng.guns.modular.system.service.ICmsArticleCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
@RequestMapping("/article")
public class ArticleController  extends BaseController {

    private String PREFIX = "/system/article/";
    @Autowired
    IArticleService articleService;
    @Autowired
    IArticleDataService articleDataService;
    @Autowired
    ICmsArticleCategoryService cmsArticleCategoryService;

    @RequestMapping(value = "")
    public String index(Model model){
        List<CmsArticleCategory> articleCategories = cmsArticleCategoryService.selectList(null);

        model.addAttribute("articleCategories",articleCategories);
        return PREFIX + "article.html";
    }


    @RequestMapping(value = "/list")
    @ResponseBody
    public List<Article> list(){
        Wrapper<Article> wrapper = new EntityWrapper<>();
        List<Article> list = articleService.selectList(wrapper);
        return list;
    }

    @RequestMapping("/article_add")
    public String add(){
        return PREFIX + "article_add.html";
    }

    @RequestMapping("/article_update/{id}")
    public String articleUpdate(@PathVariable Integer id, Model model, HttpServletRequest request){
        Article article = this.articleService.selectById(id);
        Wrapper<ArticleData> wrapper = new EntityWrapper<>();
        wrapper.eq("article_id",article.getId());
        ArticleData articleDate = this.articleDataService.selectOne(wrapper);
        model.addAttribute("article",article);
        model.addAttribute("articleData",articleDate);
        LogObjectHolder.me().set(article);
        return PREFIX + "article_edit.html";
    }
    @RequestMapping("/save")
    @ResponseBody
    @BussinessLog(value = "删除通知",key = "noticeId",dict = NoticeMap.class)
    public String save(Integer id,String title,String content,int categoryId )
    {
        if(id==null){
            Article article = new Article();
            article.setTitle(title);
            Short s =1;
            article.setCanOpen(s);
            article.setCreateUser("admin");
            article.setBrowseMum(0);
            article.setGmtCreate(new Date());
            article.setGmtModified(new Date());
            article.setCategoryId(categoryId);
            ArticleData articleData = new ArticleData();
            articleData.setContent(content);

            articleService.insert(article);
            articleData.setArticleId(article.getId());
            articleDataService.insert(articleData);

        }else
        {
            Wrapper<ArticleData> wrapper = new EntityWrapper<>();
            Article saveArticle = articleService.selectById(id);
            wrapper.eq("article_id",id);
            ArticleData saveArticleData = articleDataService.selectOne(wrapper);
            saveArticle.setTitle(title);
            saveArticle.setCategoryId(categoryId);
            saveArticle.setGmtModified(new Date());
            saveArticleData.setContent(content);

            articleService.updateAllColumnById(saveArticle);
            articleDataService.updateAllColumnById(saveArticleData);
        }
        return "ok";
    }

    @RequestMapping("/add")
    @ResponseBody()
    public String Add()
    {
        return "";
    }

    @RequestMapping("/update")
    @ResponseBody()
    public String update()
    {
        return "";
    }

    @RequestMapping("/test")
    @ResponseBody
    public Map<String,Object> test()
    {
        Map<String,Object> map = new HashMap<>();
        List<String> list = new ArrayList<>();
        map.put("error","ok");
        list.add("wang");
        list.add("zeng");
        map.put("data",list);
        return map;
    }

}
